
//app.js
App({
  globalData: {
    webContext: 'https://opservice.e-bridge.com.cn',
    openid:null
  },
  onLaunch: function () {
    var that = this
    var user = wx.getStorageSync('user') || {};
   //var userInfo = wx.getStorageSync('userInfo') || {};
     // (!user.openid || (user.expires_in || Date.now()) < (Date.now() + 600))
    if (!that.globalData.openid) {
      wx.login({
        success: function (res) {
          if (res.code) {
          /**  wx.getUserInfo({
              success: function (res) {
                var objz = {};
                objz.avatarUrl = res.userInfo.avatarUrl;
                objz.nickName = res.userInfo.nickName;
                //console.log(objz);
                wx.setStorageSync('userInfo', objz);//存储userInfo
              }
            });*/
            var webContext = that.globalData.webContext;
            wx.request({
              url: webContext + '/ebridge_h5/caigou/getUserInfo',
              data: { code: res.code },
              header: {
                'content-type': 'application/json' //默认值
              },
              success: function (res) {
                console.log(res)
                if (res.statusCode=='200'){
                  that.globalData.openid = res.data
                }
                /**    var obj = {};
                obj.openid = res.data.openid;
                obj.expires_in = Date.now() + res.data.expires_in;
                console.log(obj);
                wx.setStorageSync('user', obj);//存储openid  
                */
              }
            })
          } else {
            console.log('获取用户登录态失败！' + res.errMsg)
          }
        }
      });
    }
  }
})
